<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button
              class="mainor_tea"
              @click="(addedFormVisible = true), (type = 0)"
              >新增管理员</el-button
            >
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="关键字" prop="keywords">
              <el-input
                v-model.trim="form.keywords"
                placeholder="姓名/手机"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="角色" prop="role_id">
              <el-select
                v-model.trim="form.role_id"
                placeholder="选择角色"
                class="wid220"
              >
                <el-option label="所有" value="0"></el-option>
                <el-option
                  v-for="(role, index) in list.role"
                  :key="index"
                  :label="role"
                  :value="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table" style="min-height: 670px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column label="序号" width="80">
              <template slot-scope="scope">{{
                (current.page - 1) * current.per_page + scope.$index + 1
              }}</template>
            </el-table-column>
            <el-table-column
              label="ID"
              prop="id"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="姓名"
              prop="name"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="用户名"
              prop="username"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="手机"
              prop="mobile"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="所属角色" :show-overflow-tooltip="true">
              <template slot-scope="scope">{{
                scope.row.role_id | role_id(list.role)
              }}</template>
            </el-table-column>

            <el-table-column
              label="邮箱"
              prop="email"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="创建日期" :show-overflow-tooltip="true">
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>
            <!-- <el-table-column
              label="体验结束时间"
              width="140"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.end_time, "{y}-{m}-{d}")
              }}</template>
            </el-table-column> -->
            <el-table-column
              label="状态"
              prop="status"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <span :class="scope.row.status == 1 ? 'status1' : 'status0'">
                  {{ scope.row.status == 1 ? "正常" : "禁用" }}
                </span>
              </template>
            </el-table-column>

            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="200"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="
                    (addedFormVisible = true),
                      (type = 1),
                      (id = scope.row.id),
                      edit_assig(scope.$index)
                  "
                  >编辑</el-link
                >
                <el-link
                  type="danger"
                  v-if="scope.row.role_id > 0 ? false : true"
                  @click="(id = scope.row.id), delet(scope.$index)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :title="type == 1 ? '编辑管理员' : '新增管理员'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog450"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom1"
        label-width="100px"
      >
        <!-- <el-form-item label="结束时间" prop="end_time">
          <el-date-picker
            v-model="info.end_time"
            type="date"
            :editable="false"
            :clearable="false"
            class="time240"
            value-format="yyyy-MM-dd"
            placeholder="体验结束时间"
          ></el-date-picker>
        </el-form-item> -->
        <el-form-item label="选择角色" prop="role_id">
          <el-select
            v-model.trim="info.role_id"
            placeholder="请选择角色"
            class="wid240"
          >
            <el-option
              v-for="(role, index) in list.role"
              :key="index"
              :label="role"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名：" prop="name">
          <el-input
            v-model.trim="info.name"
            placeholder="姓名"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户名：" prop="username">
          <el-input
            v-model.trim="info.username"
            placeholder="用户名"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" :prop="type == 1 ? '' : 'password'">
          <el-input
            v-model.trim="info.password"
            show-password
            :placeholder="type == 1 ? '不填则不修改' : '填写6位以上的密码'"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机：" prop="mobile">
          <el-input
            v-model.trim="info.mobile"
            placeholder="手机"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱：" prop="email">
          <el-input
            v-model.trim="info.email"
            placeholder="邮箱"
            clearable
            class="wid240"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="info.status"
            active-color="#03A98E"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  user_index,
  user_add,
  user_edit,
  user_delete,
} from "@/api/global/globalAdmin";
import rules from "@/rules/global/globalAdmin";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";

export default {
  components: {},
  filters: {
    role_id(id, data) {
      let zs = "";
      Object.keys(data).map((e) => {
        if (id == e) {
          zs = data[e];
        }
      });
      return zs;
    },
  },
  data() {
    return {
      // 增加和编辑
      info: {
        // end_time: "",
        role_id: "",
        name: "",
        username: "",
        password: "",
        mobile: "",
        email: "",
        status: 1,
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 时间
      parseTime,
      // 验证
      rules,
      // 搜索
      form: {
        keywords: "",
        role_id: "",
      },
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    // 关闭清空表单
    close() {
      this.info = {
        // end_time: "",
        role_id: "",
        name: "",
        username: "",
        password: "",
        mobile: "",
        email: "",
      };
      this.$refs["userListFrom1"].clearValidate();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除管理员?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          user_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom1"].validate((valid) => {
        if (valid) {
          if (this.type == 1) {
            if (this.info.password != "" && this.info.password.length < 6) {
              this.$message.error("密码为6位以上");
              return;
            }
          }
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        // end_time: parseTime(this.list.list.data[index].end_time),
        role_id: String(this.list.list.data[index].role_id),
        name: this.list.list.data[index].name,
        username: this.list.list.data[index].username,
        password: "",
        mobile: this.list.list.data[index].mobile,
        email:
          this.list.list.data[index].email == null
            ? ""
            : this.list.list.data[index].email,
        status: this.list.list.data[index].status,
      };
    },
    // 新增保存
    add() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.info)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info
      );
      user_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.user_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.info, { id: this.id })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info,
        { id: this.id }
      );
      user_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.user_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.user_index();
    },
    changePage(page) {
      this.current.page = page;
      this.user_index();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.user_index();
    },
    // 列表
    user_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.form, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.form,
        this.current
      );
      user_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.user_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 120px);
}
.status1 {
  color: #03a98e;
}
.status0 {
  color: red;
}
</style>
